﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PACKETPARSERMAX20.CORE
{
    public static class ObjectExtensions
    {
        public static Coord3 ReadCoord3(this System.IO.BinaryReader reader)
        {
            return new Coord3(reader);
        }

        public static Coord3 ReadCoord3(this byte[] bytes)
        {
            return new Coord3(bytes);
        }

        public static Coord4 ReadCoord4(this System.IO.BinaryReader reader)
        {
            return new Coord4(reader);
        }

        public static Coord4 ReadCoord4(this byte[] bytes)
        {
            return new Coord4(bytes);
        }

        public static MovementJump ReadMovementJump(this System.IO.BinaryReader reader)
        {
            return new MovementJump(reader);
        }

        public static MovementFall ReadMovementFall(this System.IO.BinaryReader reader)
        {
            return new MovementFall(reader);
        }

        public static MovementTransport ReadMovementTransport(this System.IO.BinaryReader reader, int clientbuild, uint opcode)
        {
            return new MovementTransport(reader, clientbuild, opcode);
        }

        public static MovementJump ReadMovementJump(this byte[] bytes)
        {
            return new MovementJump(bytes);
        }

        public static UInt64 popGuid(this System.IO.BinaryReader reader)
        {
            byte mask = reader.ReadByte();
            if (mask == 0) { return 0; }
            byte tmpMask = 1;
            byte[] tBytes = new byte[8];

            for (byte i = 0; i < 8; i++)
            {
                if ((mask & tmpMask) > 0)
                {
                    byte tmp = reader.ReadByte();
                    tBytes[i] = tmp;
                }
                tmpMask *= 2;
            }


            return BitConverter.ToUInt64(tBytes, 0);
        }

        public static UInt64 popGuid(this byte[] bytes)
        {
            using (var ms = new System.IO.MemoryStream(bytes))
            {
                using (var br = new System.IO.BinaryReader(ms))
                {
                    byte mask = br.ReadByte();
                    byte compSize = 0;
                    byte tmpMask = 1;
                    var maskbytes = new byte[8];

                    for (byte i = 0; i < 8; i++)
                    {
                        if ((mask & tmpMask) > 0)
                        {
                            compSize++;
                            byte tmp = br.ReadByte();
                            maskbytes[i] = tmp;
                        }
                        tmpMask *= 2;
                    }

                    ms.Close();
                    br.Close();
                    return BitConverter.ToUInt64(maskbytes, 0);
                }
            }
        }


    }

}
